package org.larkdoc.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.larkdoc.constant.ViewConts;
import org.larkdoc.model.bo.DocBO;
import org.larkdoc.model.entity.DocInfo;
import org.larkdoc.model.entity.DocRelease;
import org.larkdoc.model.vo.DocReleaseVO;
import org.larkdoc.model.vo.RespBaseVO;
import org.larkdoc.service.DocReleaseService;
import org.larkdoc.service.DocService;
import org.larkdoc.util.AuthUtil;
import org.larkdoc.util.DozerUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 文档版本信息控制类
 * 
 * @author  zhangpeijun[zhangpeijun1024@qq.com]
 * @version  [v1.0.1, 2017年10月17日]
 * @see  [相关类/方法]
 * @since  [产品/模块版本]
 */
@RestController
@RequestMapping("/docRelease")
public class DocReleaseController {
    
    private final static Logger log = LoggerFactory.getLogger(DocReleaseController.class);
    
    @Autowired
    DocService                  docService;
    
    @Autowired
    DocReleaseService           docReleaseService;
    
    @RequestMapping("/versionRelease")
    public RespBaseVO versionRelease(DocReleaseVO docrVo) {
        RespBaseVO respVo = new RespBaseVO();
        if (null == docrVo || null == docrVo.getDocId() || StringUtils.isEmpty(docrVo.getVersionNo())) {
            log.info("Param is error : {}", docrVo);
            respVo.setRetCode(ViewConts.Code.PARAM_ERROR);
            respVo.setRetMsg(ViewConts.Msg.PARAM_ERROR);
            return respVo;
        }
        DocInfo docInfo = docService.selectByPrimaryKey(docrVo.getDocId());
        if (null == docInfo) {
            log.info("Not found document. id = {}", docrVo.getDocId());
            respVo.setRetCode(ViewConts.Code.PARAM_ERROR);
            respVo.setRetMsg("Not found document. id = " + docrVo.getDocId());
            return respVo;
        }
        DocRelease record = DozerUtil.conversion(docInfo, DocRelease.class);
        // 补充版本信息
        record.setDocId(docrVo.getDocId());
        record.setDescription(docrVo.getDescription());
        record.setCreateTime(new Date());
        record.setCreator(AuthUtil.getAccount());
        record.setVersionNo(docrVo.getVersionNo());
        if (!docReleaseService.checkExists(record)) {
            int result = docReleaseService.versionRelease(record);
            if (result < 1) {
                respVo.setRetCode(ViewConts.Code.FAILURE);
                respVo.setRetMsg(ViewConts.Msg.FAILURE);
            }
        } else {
            log.info("Documnet version number is exists. param: {}", docrVo);
            respVo.setRetCode(ViewConts.Code.FAILURE);
            respVo.setRetMsg("Documnet version number is exists. " + docrVo.toString());
        }
        return respVo;
    }
    
    /**
     * 获取文档集合
     * @param docrVo
     * @return
     * 
     * @returns RespBaseVO
     * @see [类、类#方法、类#成员]
     * @author  zhangpeijun[zhangpeijun1024@qq.com]<br/>
     *          2017年10月17日上午11:03:48
     */
    public RespBaseVO getList(DocReleaseVO docrVo) {
//      @RequestMapping("/getList")
        RespBaseVO respVo = new RespBaseVO();
        if (null == docrVo || null == docrVo.getDocId()) {
            log.info("Param is error : {}", docrVo);
            respVo.setRetCode(ViewConts.Code.PARAM_ERROR);
            respVo.setRetMsg(ViewConts.Msg.PARAM_ERROR);
            return respVo;
        }
        
        List<DocRelease> docReleases = docReleaseService.getDocReleasesByDocId(docrVo.getDocId(),
            AuthUtil.getAccount());
        if (CollectionUtils.isEmpty(docReleases)) {
            respVo.setRetVal("无数据或没有查看权限!");
        }
        
        return respVo;
    }
    
    @RequestMapping("/get")
    public RespBaseVO get(DocReleaseVO docrVo) {
        RespBaseVO respVo = new RespBaseVO();
        // 如未传入版本信息，则返回最新发布版本
        if (null == docrVo || null == docrVo.getDocId()) {
            log.info("Param is error : {}", docrVo);
            respVo.setRetCode(ViewConts.Code.PARAM_ERROR);
            respVo.setRetMsg(ViewConts.Msg.PARAM_ERROR);
            return respVo;
        }
        DocBO doc = docService.getAuthDocById(docrVo.getDocId(), AuthUtil.getAccount());
        DocRelease docRelease = null;
        if (null != doc) {
            String version = StringUtils.isEmpty(docrVo.getVersionNo()) ? doc.getLatestVersion()
                : docrVo.getVersionNo();
            docRelease = docReleaseService.getDocReleasesByDocIdAndVersion(docrVo.getDocId(), version);
        }
        if (null == docRelease) {
            respVo.setRetVal("无数据或没有查看权限!");
        } else {
            respVo.setRetVal(DozerUtil.conversion(docRelease, DocReleaseVO.class));
        }
        return respVo;
    }
    
    @RequestMapping("/history")
    public RespBaseVO history(DocReleaseVO docrVo) {
        RespBaseVO respVo = new RespBaseVO();
        if (null == docrVo || null == docrVo.getDocId()) {
            log.info("Param is error : {}", docrVo);
            respVo.setRetCode(ViewConts.Code.PARAM_ERROR);
            respVo.setRetMsg(ViewConts.Msg.PARAM_ERROR);
            return respVo;
        }
        DocBO doc = docService.getAuthDocById(docrVo.getDocId(), AuthUtil.getAccount());
        List<DocRelease> docRelease = null;
        if (null != doc) {
            docRelease = docReleaseService.getHistory(docrVo.getDocId());
        }
        if (null == docRelease) {
            respVo.setRetVal("无数据或没有查看权限!");
        } else {
            respVo.setRetVal(DozerUtil.conversionList(docRelease, DocReleaseVO.class));
        }
        return respVo;
    }
}
